<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class control extends base{

	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('pms');
		$this->load('user');
	}

	function dodefault(){
		$this->dobox();
	}
	
	function dosendmessage(){
		if(is_numeric($this->get[3])){
			$pms=$_ENV['pms']->get_pms($this->get[3]);
			switch($this->get[2]){
				case 'reply':
					$subject='Re:'.$pms['subject'];
					$sendto=$pms['from'];
					break;
				case 'forward':
					$subject=$pms['subject'];
					$sendto='';
					break;
				case 'drafts':
					$subject=$pms['subject'];
					$sendto=$pms['username'];
			}
			$message=$pms['message'];
			$this->view->assign('subject',$subject);
			$this->view->assign('message',$message);
			$this->view->assign('sendto',$sendto);
		}
		if(isset($this->post['submit'])){
			$sendto=$this->post['sendto'];
			$subject=$this->post['subject'];
			$content=$this->post['content'];
			if (WIKI_CHARSET == 'GBK'){
				$sendto=string::hiconv($sendto);
				$subject=string::hiconv($subject);
				$content=string::hiconv($content);
			}
			$sendto=htmlspecialchars($sendto);
			$subject=htmlspecialchars($subject);
			$content=htmlspecialchars($content);
			$isdraft=$this->post['checkbox'];
			$sendreturn=$_ENV['pms']->send_message($sendto,$subject,$content,$this->user['username'],$this->user['uid'],$isdraft);
			if($sendreturn){
				$_ENV['user']->add_credit($this->user['uid'],'user-pms',$this->setting['credit_pms']);
				$msg=$this->view->lang['viewDocTip1'];
			}else{
				$msg=$this->view->lang['viewDocTip3'];
			}
			if ($this->post['submit'] == 'ajax'){
				if ($sendreturn) {
					exit('OK');
				} else {
					exit('error');
				}
			} else {
				$this->message($msg,'index.php?pms',0);
			}
		}
		$this->view->display('sendmessage');
	}
	
	function docheckrecipient(){
		$sendto=string::hiconv($this->post['sendto']);
		$sendto = string::hiconv($sendto);
		$send=explode(',',$sendto);
		if(count($send)>10){
			$this->message($this->view->lang['fullsend'],'',2);
		}
		$checkreturn=$_ENV['pms']->check_recipient($sendto,0);
		if($checkreturn==''){
			$msg='OK';
		}else{
			$msg=$checkreturn.' '.$this->view->lang['loginTip3'];
		}
		$this->message($msg,'',2);
	}
	
	function doblacklist(){
		if(isset($this->post['blacklist'])){
			$blacklist=htmlspecialchars(string::stripscript($this->post['blacklist']));
			if($blacklist==''){
				if($_ENV['pms']->get_blacklist($this->user['uid'])==''){
					$msg=1;
				}else{
					if($_ENV['pms']->remove_blacklist($this->user['uid'])){
						$msg=2;
					}else{
						$msg=3;
					}
				}
			}else{
				if($_ENV['pms']->add_blacklist($blacklist,$this->user['uid'])){
					$msg=2;
				}else{
					$msg=3;
				}
			}
			$this->message($msg,'',2);
		}else{
			$this->view->assign('blacklist',$_ENV['pms']->get_blacklist($this->user['uid']));
			$this->view->display('blacklist');
		}
	}
	
	function dobox(){
		$page = max(1, intval($this->get[3]));
		$num = isset($this->setting['list_prepage'])?$this->setting['list_prepage']:20;
		$start_limit = ($page - 1) * $num;
		
		$count=$_ENV['pms']->get_totalpms($this->user['uid'],$this->get[2]);
		$count=($count>200 && $this->get[2]=='')?200:$count;
		$pmslist=$_ENV['pms']->get_box($this->user['uid'],$start_limit,$num,$this->get[2]);
		$departstr=$this->multi($count, $num, $page,"pms-box-".$this->get[2]);
		
		$this->view->assign('departstr',$departstr);
		$this->view->assign('pmslist',$pmslist);
		$this->view->assign('type',$this->get[2]);
		$this->view->assign('count',$count);
		$this->view->display('box');
	}
	
	function doremove(){
		if($this->get[2]==1){	
			if($this->post['id']){
				if($_ENV['pms']->update_pms($this->post['id'],$this->post['type'])){
					$msg=$this->get_newpms($this->user['uid']);
				}else{
					$msg=false;
				}
				$this->message($msg,'',2);
			}
		}else{
			$removeid=$this->post['checkid'];
			$num=count($removeid);
			$result=true;
			$type=($this->get[3]=='')?1:2;
			for($i=0;$i<$num;$i++){
				if(!$_ENV['pms']->update_pms($removeid[$i],$type)){
					$result=false;
				}
			}
			if($result){
				$this->header('pms-box-'.$this->get[3]);	
			}else{
				$this->message($this->view->lang['delpmserror'],'index.php?pms',0);
			}
		}
	}
	
	function dosetread(){
		if($this->post['id']){
			if($_ENV['pms']->set_read($this->post['id'])){
				$msg=$this->newpms($this->user['uid']);
			}else{
				$msg=false;
			}
			$this->message($msg,'',2);
		}
	}
}
?>